class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        int len = words[0].size(), n = words.size();
        unordered_map<string, int> exp;
        for (auto e : words) {
            exp[e]++;
        }

        vector<int> res;

        for (int i = 0; i < s.size(); ++i) {
            if (s.size() - i + 1 < n * len) break;

            bool flag = false;
            unordered_map<string, int> hash;
            for (int j = i; j < i + n * len; j += len) {
                string str = s.substr(j, len);
                if (exp.find(str) == exp.end()) {
                    flag = true;
                    break;
                }
                else {
                    hash[str]++;
                }
            }
            if (!flag && hash == exp) {
                res.push_back(i);
            }
        }
        return res;
    }
};